package edu.mse.camel.client.posterboard.ui.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.actions.ActionFactory;

import edu.mse.camel.client.posterboard.ui.actions.ZoomToFit;

public class PosterBoardContextMenuProvider extends ContextMenuProvider {
	
	private List<IContextMenuHelper> helpers;
	
	private ActionRegistry actionRegistry;
	
	/**
	 * Instantiate a new menu context provider for the specified EditPartViewer 
	 * and ActionRegistry.
	 * @param viewer	the editor's graphical viewer
	 * @param registry	the editor's action registry
	 * @throws IllegalArgumentException if registry is <tt>null</tt>. 
	 */
	public PosterBoardContextMenuProvider(EditPartViewer viewer, ActionRegistry registry) {
		super(viewer);
		if (registry == null) {
			throw new IllegalArgumentException();
		}
		actionRegistry = registry;
		helpers = new ArrayList<IContextMenuHelper>();
	}
	
	@Override
	public void buildContextMenu(IMenuManager menu) {
		GEFActionConstants.addStandardActionGroups(menu);
		
		for (IContextMenuHelper aHelper : helpers) {
			aHelper.appendToMenu(menu, actionRegistry);
		}
		
		IAction action = actionRegistry.getAction(ZoomToFit.ID);
		menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);
		
	}
	
	public void addHelper(IContextMenuHelper helper) {
		if (!helpers.contains(helper)) {
			helpers.add(helper);
		}
	}

}
